﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.core.services;
using mscook.mvc.Models;
using mscook.mvc.core;
namespace mscook.mvc.Controllers
{
    public class BaseController : Controller
    {
        public IList<ActionBonusSetting> ActionBonusSettings
        {
            get;
            protected set;
        }
        public IList<ResourceBonusSetting> ResourceBonusSettings
        {
            get;
            protected set;
        }
        public IActionBonusSettingService ActionBonusSettingService
        {
            get;
            protected set;
        }
        public IResourceBonusSettingService ResourceBonusSettingService
        {
            get;
            protected set;
        }
        public ILoginUserService LoginUserService
        {
            get;
            protected set;
        }
        public LoginUser CurrentUser
        {
            get
            {
                LoginUser loginUser;


                var loginRepo = this.LoginUserService;

                string loginId = this.HttpContext.User.Identity.Name;

                loginUser = loginRepo.SelectLoginUserByLoginName(loginId);


                return loginUser;
            }
        }
        private IMagazineService iMagazineService = UnityHelper.UnityToT<IMagazineService>();
        private IResourceTypeRepository iResourceTypeRepository = UnityHelper.UnityToT<IResourceTypeRepository>();
        private IRelativeInfoRepository iRelativeInfoRepository = UnityHelper.UnityToT<IRelativeInfoRepository>();
        private IRelativeLinkRepository iRelativeLinkRepository = UnityHelper.UnityToT<IRelativeLinkRepository>();
        private IResourceBonusSettingRepository iResourceBonusSettingRepository = UnityHelper.UnityToT<IResourceBonusSettingRepository>();
        private ICommentRepository iCommentRepository = UnityHelper.UnityToT<ICommentRepository>();

        public BaseController(IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService)
        {
            ActionBonusSettings = actionBonusSettingService.Select();
            ResourceBonusSettings = resourceBonusSettingService.SelectAll();
            this.ActionBonusSettingService = actionBonusSettingService;
            this.ResourceBonusSettingService = resourceBonusSettingService;
            this.LoginUserService = loginUserService;
        }
        protected void InitNavBar()
        {
            ViewData[ViewDataKeys.ISLOGIN] = CurrentUser != null;
            ViewData[ViewDataKeys.USERNAME] = CurrentUser != null ? CurrentUser.LoginName : "";
            ViewData[ViewDataKeys.BONUS] = CurrentUser != null ? CurrentUser.Bonus.ToString() : "0";

        }

        protected void InitSourceType(BaseModel baseModel, PurchasedCategory category)
        {
            baseModel.ResourceTypes = iResourceTypeRepository.GetAllParentResourceType(category.ToString()).OrderByDescending(o => o.EntityIndex).ToList();
            baseModel.ChildResourceTypes = iResourceTypeRepository.GetAllChildResourceType(category.ToString()).OrderByDescending(o => o.EntityIndex).ToList();
        }

        protected IList<string> GetRsourceIdsByTypes(IList<string> typeIds, PurchasedCategory category)
        {
            return iResourceTypeRepository.GetResourceByTypes(category, typeIds);
        }

        public IList<RelativeInfo> GetRelativeInfo(string id)
        {
            return iRelativeInfoRepository.GetByResourceId(id);
        }

        public Magazine GetPushMagazine()
        {
            return iMagazineService.GetRecent(0, 1).SingleOrDefault();
        }

        public IList<RelativeLink> GetRelativeLinks(IList<string> resourceIds)
        {
            return iRelativeLinkRepository.GetByResourceIds(resourceIds);
        }

        public IList<ResourceBonusSetting> GetBonusByResourceIds(IList<string> resourceIds)
        {
            return iResourceBonusSettingRepository.SelectByResourceIds(resourceIds);
        }

       




    }
}